#include "NameForm.h"
#include <QPainter>
#include <QBrush>
#include <QFont>
#include <QMouseEvent>

NameForm::NameForm(ElementForm* parent)
	: ContainsExitForm(parent),_name("")
	,_nameRect(145,78, 259, 24), _isMoveFlag(false)
	,_displayNum(3)
{
}

QString NameForm::name() const
{
	return _name;
}

void NameForm::setName(const QString& name)
{
	if (name != _name) {
		_name = name;
		repaint();
	}

}



int NameForm::diplayNum() const
{
	return _displayNum;
}

void NameForm::setDisplayNum(int displayNum)
{
	if (displayNum != _displayNum && displayNum >= 0)
	{
		_displayNum = displayNum;
	}
	 updateParam();
}


void NameForm::paintEvent(QPaintEvent* event)
{
	QPainter painter(this);
	painter.setRenderHints(QPainter::Antialiasing|QPainter::TextAntialiasing, true);
	CommonFunc::drawFormName(&painter, _nameRect,_name);
	ContainsExitForm::paintEvent(event);
}


void NameForm::mouseMoveEvent(QMouseEvent* event)
{
	if (_isPressed) {
		_endPos = event->pos();
		int offset = _endPos.y() - _startPos.y();
		if (qAbs(offset) > 50) {
			_isMoveFlag = true;
			_startPos = _endPos;
			if (offset > 50) {
				left();
			}
			else if (offset < -50) {
				right();
			}
		}
	}
	ContainsExitForm::mouseMoveEvent(event);
}

void NameForm::mouseReleaseEvent(QMouseEvent* event)
{
	if (_isMoveFlag) {
		_isMoveFlag = false;
		return;
	}
	if (_elements.size() > 0) {
		if (_elements.data() != nullptr) {
			QRect rect = _elements.data()->rect();
			if (rect.contains(event->pos()) && rect.contains(_startPos)) {
				shortPress();
				return;
			}
		}
	}
	ContainsExitForm::mouseReleaseEvent(event);
}

void NameForm::updateParam()
{

}
